package cn.xz.thread_demo;

public class ThreadLifeCycleDemo {
    private static Integer storeNum = 10;

    public synchronized void sale(){
        if (storeNum>3){
            System.out.println("售卖"+Thread.currentThread().getName());
            storeNum-=4;
            notifyAll();
        }else {
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public synchronized void produce(){
        if (storeNum <= 3){
            System.out.println("生产"+Thread.currentThread().getName());
            storeNum+=4;
            notifyAll();
        }else {
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
